package frame.options.virtualNetworks;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import model.components.networks.network.VirtualNetwork;
import frame.MopraFrame;
import frame.networks.tables.VirtualNetworksTableModel;
import frame.networks.tables.VirtualNetworksTable;
import frame.networks.viewers.VirtualNetworksVisualizationViewersTabbedPane;
import frame.options.virtualNetworks.create.CreateVirtualNetworksWizard;

@SuppressWarnings("serial")
public class CreateVirtualNetworksMenuItem extends JMenuItem implements ActionListener
{
	private MopraFrame mopraFrame;
	public MopraFrame getFrame(){ return mopraFrame; }
	public void setFrame( MopraFrame mopraFrame ){ this.mopraFrame = mopraFrame; }

	public CreateVirtualNetworksMenuItem( MopraFrame mopraFrame )
	{
		this.setFrame( mopraFrame );
		
		this.setText( "Create" );
		this.setMnemonic( KeyEvent.VK_C );
		this.setIcon( new ImageIcon( this.getClass().getResource( "/icons/create.png" ) ) );
		this.addActionListener( this );
	}
	
	@Override
	public void actionPerformed( ActionEvent actionEvent )
	{
		this.getFrame().getNetworksPanel().getNetworksTablesTabbedPane().setSelectedIndex( 1 );
		
		CreateVirtualNetworksWizard createVirtualNetworksWizard = new CreateVirtualNetworksWizard( this.getFrame() );
		
		if( createVirtualNetworksWizard.showModalDialog() == 0 )
		{
			List< VirtualNetwork > simulatorVirtualNetworks = this.getFrame().getSimulator().getVirtualNetworks();
			VirtualNetworksVisualizationViewersTabbedPane virtualNetworksVisualizationViewersTabbedPane = this.getFrame().getNetworksPanel().getVirtualNetworksVisualizationViewersTabbedPane();
			VirtualNetworksTable virtualNetworksTable = this.getFrame().getNetworksPanel().getNetworksTablesTabbedPane().getVirtualNetworksTable();
			VirtualNetworksTableModel virtualNetworksTableModel = (VirtualNetworksTableModel) virtualNetworksTable.getModel();
			SaveVirtualNetworksMenuItem saveVirtualNetworksMenuItem = this.getFrame().getOptions().getVirtualNetworksMenu().getSaveVirtualNetworksMenuItem();
			
			simulatorVirtualNetworks.clear();
			virtualNetworksVisualizationViewersTabbedPane.removeAll();
			virtualNetworksTable.removeAll();
			
			for( VirtualNetwork virtualNetwork: createVirtualNetworksWizard.getVirtualNetworks() )
			{
				simulatorVirtualNetworks.add( virtualNetwork );
				virtualNetworksVisualizationViewersTabbedPane.addVirtualNetworkTab( virtualNetwork );
				virtualNetworksTableModel.addRow( new Object[]{ virtualNetwork.getName(), virtualNetwork.getStartingDate(), virtualNetwork.getEndingDate(), virtualNetwork.getState() } );
			}
			
			virtualNetworksTableModel.refresh();
			
			if( simulatorVirtualNetworks.size() == 0 )
				saveVirtualNetworksMenuItem.setEnabled( false );
			else
				saveVirtualNetworksMenuItem.setEnabled( true );
		}
	}
	
}
